package cn.com.cym.gamezao.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericSuperclassUtil {
    /*
     * 获取泛型类Class对象，不是泛型类则返回null
     * 只能获取到T
     */
    public static Class<?> getActualTypeArgument(Class<?> clazz) {
        Class<?> entityClazz = null;
        Type genericSuperclass = clazz.getGenericInterfaces()[0];
        if (genericSuperclass instanceof ParameterizedType) {
            ParameterizedType ownerType = ((ParameterizedType) genericSuperclass);
            Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass)
                    .getActualTypeArguments();
            if (actualTypeArguments != null && actualTypeArguments.length > 0) {
                entityClazz = (Class<?>) actualTypeArguments[0];
            }
        }
        return entityClazz;
    }
}
